package com.bpmn.flow.controller;

import com.bpmn.common.annotation.ResponseInfoWrapper;
import com.bpmn.common.pojo.PageResult;
import com.bpmn.flow.controller.query.ProcessInstanceQuery;
import com.bpmn.flow.controller.vo.BpmnModelViewVo;
import com.bpmn.flow.controller.vo.InstanceVo;
import com.bpmn.flow.service.InstanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rain
 */
@Tag(name = "流程实例相关")
@RestController
@RequiredArgsConstructor
@ResponseInfoWrapper
@RequestMapping(value = "instance")
public class InstanceController {

    private final InstanceService instanceService;

    /**
     * 流程定义查询接口
     */
    @Operation(summary = "流程实例查询接口")
    @PostMapping(value = "/list")
    public PageResult<InstanceVo> getProcessInstancePage(@RequestBody ProcessInstanceQuery processInstanceQuery) {
        return instanceService.getProcessInstancePage(processInstanceQuery);
    }

    @Operation(summary = "获取流程实例的BPMN模型视图")
    @PostMapping(value = "/getBpmnModelView")
    @Parameter(name = "id", description = "流程实例的编号", required = true)
    public BpmnModelViewVo getBpmnModelView(@RequestParam(value = "id") String id) {
        return instanceService.getBpmnModelView(id);
    }

    @PostMapping("/stopProcessInstance")
    @Operation(summary = "终止流程实例", description = "管理员终止流程")
    @Parameter(name = "id", description = "流程实例的编号", required = true)
    public Boolean stopProcessInstance(@RequestParam(value = "id") String id) {
        instanceService.stopProcessInstance(id);
        return true;
    }

    @Operation(summary = "复活流程实例", description = "管理员复活已终止的流程实例")
    @PostMapping("/restartProcessInstance")
    @Parameter(name = "id", description = "流程实例ID", required = true)
    @Parameter(name = "activityIds", description = "要复活的节点列表", required = true)
    public Boolean restartProcessInstance(@RequestParam(value = "id") String id,
                                          @RequestParam(value = "activityIds") List<String> activityIds) {
        instanceService.restartProcessInstance(id, activityIds);
        return true;
    }

}
